import numpy as np
import pandas as pd
import xgboost as xgb

df_train = pd.read_csv('./data/used_car_train_20200313.csv',sep=' ')
df_test = pd.read_csv('./data/used_car_testB_20200421.csv',sep=' ')

#时间中有些异常值，先把月份中的00替换为01
def normalize_date(x):
    new_x = x
    month = int(x[4:6])
    if month < 1:
        new_x = x[0:4] + "01" + x[6:8]

    return new_x


df_train.regDate = df_train.regDate.astype(str).map(normalize_date).astype('int64')
df_test.regDate = df_test.regDate.astype(str).map(normalize_date).astype('int64')

#转换为时间diff
min_date = pd.to_datetime('19910101', format='%Y%m%d')
df_train['regTime'] = (pd.to_datetime(df_train['regDate'], format='%Y%m%d', errors='coerce') - min_date).dt.days

# 汽车售卖时间
df_train['createTime'] = (pd.to_datetime(df_train['creatDate'], format='%Y%m%d', errors='coerce')- min_date).dt.days

#汽车注册时间
df_train['usedTime'] = df_train['createTime']  - df_train['regTime']

#测试集数据
df_test['regTime'] = (pd.to_datetime(df_test['regDate'], format='%Y%m%d', errors='coerce') - min_date).dt.days

# 汽车售卖时间
df_test['createTime'] = (pd.to_datetime(df_test['creatDate'], format='%Y%m%d', errors='coerce')- min_date).dt.days

#汽车注册时间
df_test['usedTime'] = df_test['createTime']  - df_test['regTime']


# print(df_train.isnull().sum())

#用-1填充
df_train.fillna(-1, inplace=True)
df_test.fillna(-1, inplace=True)

# print(df_train.isnull().sum())

# #用KNN方法填充
# from sklearn.impute import KNNImputer
# imputer = KNNImputer(n_neighbors=3)
# df_train_copy = df_train.copy(deep=True)
# df_train_copy[['model','bodyType','fuelType','gearbox','v_1','v_2','v_3','v_4','v_5','v_6','v_7','v_8','v_9','v_10','v_11','v_12','v_13','v_14']] = \
# imputer.fit_transform(df_train_copy[['model','bodyType','fuelType','gearbox','v_1','v_2','v_3','v_4','v_5','v_6','v_7','v_8','v_9','v_10','v_11','v_12','v_13','v_14']])
#
# df_train[['model','bodyType','fuelType','gearbox']] = df_train_copy[['model','bodyType','fuelType','gearbox']]

df_train.notRepairedDamage = df_train.notRepairedDamage.replace('-','0.0').astype('float64')
df_test.notRepairedDamage = df_test.notRepairedDamage.replace('-','0.0').astype('float64')
#
# print(df_train.info())
# print(df_test.info())
y_data = df_train.price
X_data = df_train.drop(columns=['price'])
#
X_test = df_test


model = xgb.XGBRegressor(n_estimators=150, learning_rate=0.1, max_depth=7)
model.fit(X_data,y_data)

y_predict = model.predict(X_test)

result = pd.DataFrame()
result['SaleID'] = X_test['SaleID']
result['price'] = y_predict

result.loc[result['price']<11,'price'] = 11

print(result['price'].describe())
result.to_csv('./result_xgb_20201101.csv', index=False)